import random
import time

from PyQt5.QtWidgets import QWidget, QPushButton


class Mode(QWidget):
    """管理音乐播放模式的类"""
    def __init__(self, MP3Player):
        super().__init__()
        # 基本控件
        self.PlayModeBtn = QPushButton(self)
        self.playMode = 0
        # 取自MP3Player中的控件
        self.file = MP3Player.file
        self.play = MP3Player.play
        self.ms = MP3Player.ms
        self.player = MP3Player.player
        # 初始化
        self.PlayModeBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/sequential.png)}")
        self.PlayModeBtn.setFixedSize(36, 36)
        # SignalSlot
        self.PlayModeBtn.clicked.connect(self.playModeSet)

    def playByMode(self):
        """根据播放模式自动播放，并刷新进度条"""
        # 刷新进度条
        if (not self.file.is_pause) and (not self.play.is_switching):
            self.ms.slider.setMinimum(0)
            self.ms.slider.setMaximum(self.player.duration())   #duration()获取当前媒体播放总时间
            self.ms.slider.setValue(self.ms.slider.value() + 1000)
        self.ms.startTimeLabel.setText(time.strftime('%M:%S', time.localtime(self.player.position()/1000)))
        self.ms.endTimeLabel.setText(time.strftime('%M:%S', time.localtime(self.player.duration()/1000)))
        # 顺序播放
        if (self.playMode == 0) and (not self.file.is_pause) and (not self.play.is_switching):
            if self.file.musicList.count() == 0:
                return
            if self.player.position() == self.player.duration():
                self.play.nextMusic()
        # 单曲循环
        elif (self.playMode == 1) and (not self.file.is_pause) and (not self.play.is_switching):
            if self.file.musicList.count() == 0:
                return
            if self.player.position() == self.player.duration():
                self.play.is_switching = True
                self.play.setCurPlaying()
                self.ms.slider.setValue(0)
                self.play.playMusic()
                self.play.is_switching = False
        # 随机播放
        elif (self.playMode == 2) and (not self.file.is_pause) and (not self.play.is_switching):
            if self.file.musicList.count() == 0:
                return
            if self.player.position() == self.player.duration():
                self.play.is_switching = True
                self.file.musicList.setCurrentRow(random.randint(0, self.file.musicList.count()-1))
                self.play.setCurPlaying()
                self.ms.slider.setValue(0)
                self.play.playMusic()
                self.play.is_switching = False


    def playModeSet(self):
        """播放模式设置"""
        # 设置为单曲循环模式
        if self.playMode == 0:
            self.playMode = 1
            self.PlayModeBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/singleloop.png)}")
        # 设置为随机播放模式
        elif self.playMode == 1:
            self.playMode = 2
            self.PlayModeBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/random.png)}")
        # 设置为顺序播放模式
        elif self.playMode == 2:
            self.playMode = 0
            self.PlayModeBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/sequential.png)}")